package com.axend.aerosense.login.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.axend.aerosense.base.activity.MvvmBaseActivity;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.base.utils.DisplayUtils;
import com.axend.aerosense.base.viewmodel.BaseViewModel;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.WsApi;
import com.axend.aerosense.common.bean.ServerInfoBean;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.login.BuildConfig;
import com.axend.aerosense.login.R;
import com.axend.aerosense.login.databinding.LoginActivityIpSettingsBinding;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class LoginIpSettingsActivity extends MvvmBaseActivity<LoginActivityIpSettingsBinding, BaseViewModel> {

    @Override
    protected BaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected int getBindingVariable() {
        return 0;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.login_activity_ip_settings;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!checkDebugMode()) {
            this.finish();
            return;
        }

        viewDataBinding.loginEdAppServer.setText(getCurrentAppServerInfo());
        viewDataBinding.loginEdDeviceServer.setText(getCurrentDeviceServerInfo());
        viewDataBinding.loginEdWsServer.setText(getCurrentWsServerInfo());

        //刷新
        viewDataBinding.loginTvRefresh.setOnClickListener(v -> {
            refreshCurrentServerInfo();
            ToastUtils.showShort("Refresh Success!");
        });
        //保存
        viewDataBinding.loginBtnSave.setOnClickListener(v -> {
            saveServerInfo();
            refreshCurrentServerInfo();
        });
        //清除全部
        viewDataBinding.loginTvClearAll.setOnClickListener(v -> {
            viewDataBinding.loginEdAppServer.setText("");
            viewDataBinding.loginEdDeviceServer.setText("");
            viewDataBinding.loginEdWsServer.setText("");
        });
        //返回
        viewDataBinding.loginBtnBack.setOnClickListener(v -> LoginIpSettingsActivity.this.finish());

    }

//    private void showSaveHistory() {
//        List<ServerInfoBean> serverInfoBeans = loadServerInfo();
//        if (null == serverInfoBeans || serverInfoBeans.isEmpty()) {
//            return;
//        }
//
//        for (int i = 0; i < serverInfoBeans.size(); i++) {
//            TextView textView = createTextView();
//            textView.setTag(serverInfoBeans);
//            textView.setOnClickListener(v -> {
//                Object tag = v.getTag();
//                if (tag instanceof ServerInfoBean) {
//                    ServerInfoBean bean = (ServerInfoBean) tag;
//                    viewDataBinding.loginEdAppServer.setText(bean.getAppServer());
//                    viewDataBinding.loginEdWsServer.setText(bean.getWsServer());
//                }
//            });
//
//            String text = "App Server: " + getCurrentAppServerInfo() + "\n"
//                    + "WS Server: " + getCurrentWsServerInfo();
//            textView.setText(text);
//            viewDataBinding.loginLlServerList.addView(textView);
//        }
//    }

//    private TextView createTextView(){
//        TextView tv = new TextView(this);
//        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
//                ViewGroup.LayoutParams.WRAP_CONTENT,
//                ViewGroup.LayoutParams.WRAP_CONTENT);
//        layoutParams.topMargin = DisplayUtils.dip2px(20);
//        tv.setLayoutParams(layoutParams);
//        tv.setPadding(
//                DisplayUtils.dip2px(5),
//                DisplayUtils.dip2px(5),
//                DisplayUtils.dip2px(5),
//                DisplayUtils.dip2px(5)
//        );
//        tv.setBackground(getResources().getDrawable(R.drawable.common_blue_corner_rectangle_15_shape));
//        tv.setTextColor(getResources().getColor(R.color.white));
//
//        return tv;
//    }

    private void saveServerInfo(){
        String appServer = viewDataBinding.loginEdAppServer.getText().toString();
        String deviceServer = viewDataBinding.loginEdDeviceServer.getText().toString();
        String wsServer = viewDataBinding.loginEdWsServer.getText().toString();
        if (TextUtils.isEmpty(appServer) || TextUtils.isEmpty(wsServer) || TextUtils.isEmpty(deviceServer)) {
            ToastUtils.showShort("App server, device server and WS server cannot be empty!");
            return;
        }

        if (!deviceServer.contains(",")) {
            ToastUtils.showLong("Save failed! \n Device server format:  server,port");
            return;
        }

        UrlApi.setBaseUrl(appServer);
        UrlApi.setRadarTcpServerUrl(deviceServer);
        WsApi.setBaseUrl(wsServer);

        saveData(new ServerInfoBean(appServer, deviceServer, wsServer));
        ToastUtils.showShort("Save Success!");
    }

    private void saveData(ServerInfoBean bean){
//        List<ServerInfoBean> beanList = loadServerInfo();
//        if (null == beanList || beanList.isEmpty()) {
//            beanList = new ArrayList<>();
//        }
//
//        ServerInfoBean temp = null;
//        for (ServerInfoBean serverInfoBean : beanList) {
//            serverInfoBean.setUse(false);
//            if (serverInfoBean.equals(bean)) {
//                temp = serverInfoBean;
//            }
//        }
//        if (null != temp) {
//            temp.setUse(true);
//        } else {
//            beanList.add(bean);
//        }

        if (null != bean) {
            MmkvHelper.getInstance().putObject(Gobal.SAVE_KEY.DEBUG_SERVER_INFO_SAVE_KEY, bean);
        }
    }

    private ServerInfoBean loadServerInfo(){
        ServerInfoBean bean = MmkvHelper.getInstance().getObject(Gobal.SAVE_KEY.DEBUG_SERVER_INFO_SAVE_KEY, ServerInfoBean.class);
        return bean;
    }

    private void refreshCurrentServerInfo(){
        String text = "App Server:  " + getCurrentAppServerInfo() + "\n"
                + "Device Server:  " + getCurrentDeviceServerInfo() + "\n"
                + "WS Server:  " + getCurrentWsServerInfo();
        viewDataBinding.loginCurrentServerInfo.setText(text);
    }

    private String getCurrentAppServerInfo(){
        return UrlApi.getBaseUrl();
    }

    private String getCurrentDeviceServerInfo(){
        return UrlApi.getRadarTcpServerUrl();
    }

    private String getCurrentWsServerInfo(){
        return WsApi.getBaseUrl();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!checkDebugMode()) {
            this.finish();
            return;
        }
        refreshCurrentServerInfo();
    }

    private boolean checkDebugMode(){
        boolean isDebug = BuildConfig.DEBUG;
        return isDebug;
    }
}
